using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using Customer;
using System.Net.Sockets;
using System.Net;


namespace WindowsFormsApplication1
{
    public partial class FirstForm : Form
    {              
         BitArray status;         
         Person person;
         string[] drinkPrice;

        // variables for updating drink prices         
         string ipAddress = "10.40.136.93";//"10.40.163.142";//"10.40.167.6";//"10.40.137.203";//"10.254.254.253";//"10.40.163.87";
         int portNumber = 9050;//50005;

         // variables to send data to bartender
         string stringData;
         byte[] data;
         IPEndPoint ipep;
         Socket server;
        
         // variables to receive data from bartender
         IPEndPoint sender;
         EndPoint Remote;
         int recv;

         bool bartenderAvailable= false;
                             
        public FirstForm(Person Person, BitArray Status)
        {
            person = Person;
            status = Status; 
            InitializeComponent();
            cardNumber.Text = "";            
        }

        private void FirstForm_Load(object sender, EventArgs e)
        {
            cardNumber.Text = person.CardNumber;

            string updatedDrinks = getUpdatedDrinks();
            char[] delimiterChars = { '/' };
            drinkPrice = updatedDrinks.Split(delimiterChars);
            person.Drink = drinkPrice;
        }
        
        private void cardNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                person = new Person();
                person.CardNumber = cardNumber.Text;
                person.Drink = drinkPrice;
                if (person.CardNumber.Length > 0)
                {
                    // if the bartender swipes card, bring up the bartender screen
                    if ((person.CardNumber == ";014240310265564?") |
                        (person.CardNumber == ";014015890365564?") |
                        (person.CardNumber == ";016107080365564?") |
                        (person.CardNumber == ";013546550565564?"))
                    {

                        cardNumber.Clear();
                        string updatedDrinks = getUpdatedDrinks();
                        char[] delimiterChars = { '/' };
                        drinkPrice = updatedDrinks.Split(delimiterChars);
                        person.Drink = drinkPrice;
                        Bartender_1 bartenderForm = new Bartender_1(person, status);
                        cardNumber.Clear();
                        bartenderForm.ShowDialog();
                    }
                    // otherwise bring up form to let user decide what type of drink they are ordering
                    else if (person.CardNumber[0].ToString() == ";")
                    {

                        TypeSelection ts = new TypeSelection(person, status);
                        cardNumber.Clear();
                        string updatedDrinks = getUpdatedDrinks();
                        char[] delimiterChars = { '/' };
                        drinkPrice = updatedDrinks.Split(delimiterChars);
                        person.Drink = drinkPrice;
                        if (bartenderAvailable)
                        {
                            ts.ShowDialog();
                        }
                    }
                    else
                    {
                        cardNumber.Clear();
                        MessageBox.Show("Please Swipe A Valid Credit Card");
                    }
                }
            }
        }
        

        public string getUpdatedDrinks()
        {
            data = new byte[1024];
            ipep = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber);
            server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            data = Encoding.ASCII.GetBytes("!");
            // try sending the order to the bartender
            try
            {
                server.SendTo(data, data.Length, SocketFlags.None, ipep);
                bartenderAvailable = true;
            }
                
            catch (SocketException ex)
            {
                string message = ex.Message;
                bartenderAvailable = false;
                System.Windows.Forms.MessageBox.Show("Bartender Not Available, Please Request Help");
                
            }

            sender = new IPEndPoint(IPAddress.Any, 0);
            Remote = (EndPoint)sender;
            data = new byte[1024];

            // try to receive the data send from the server
            try
            {
                recv = server.ReceiveFrom(data, ref Remote);
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                bartenderAvailable = true;
                errorLabel.Visible = false;
                connectionButton.Visible = false;
                helpButton.Visible = true;
                return stringData;
            }
            catch (SocketException ex)
            {
                string message = ex.Message;
                //System.Windows.Forms.MessageBox.Show("No Bartender Available, Please Ask For Assistance");
                stringData = "No Host Detected";
                errorLabel.Visible = true;
                connectionButton.Visible = true;
                bartenderAvailable = false;
                helpButton.Visible = false;
                return stringData;
            }
        }

        private void helpButton_Click(object sender, EventArgs e)
        {            
            string helpMessage = "To begin, you must swipe your credit card, this will store " +
                "your payment information into a local database, all drinks you order tonight will " +
                " be charged using your card information";
            HelpForm helpForm = new HelpForm(helpMessage);
            helpForm.ShowDialog();
            cardNumber.Focus();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void connectionButton_Click(object sender, EventArgs e)
        {
            cardNumber.Text = person.CardNumber;
            string updatedDrinks = getUpdatedDrinks();
            char[] delimiterChars = { '/' };
            drinkPrice = updatedDrinks.Split(delimiterChars);
            person.Drink = drinkPrice; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
